<?php

/**
 * template_content actions.
 *
 * @package    master_directory
 * @subpackage template_content
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class template_contentActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->content_types = Doctrine_Core::getTable('ContentType')->retreiveActiveContentTypes();
    
    $template_id = $request->getParameter('template_id');
    
    $this->template = Doctrine_Core::getTable('Template')->find(array($template_id));
    
    $deletable = false;
    
    $template_content_arr = array();
    
    
    foreach ($this->content_types as $content_type){
    	$templateContent = Doctrine_Core::getTable('TemplateContent')->getTemplateContent($template_id,$content_type->getId() );
    	
    	$template_content_arr[$content_type->getId()] = $templateContent;
    	//echo 'content_type: ' . $content_type->getId(); exit();
    	
     	if($templateContent != null){
    		$deletable = true;
    	}
    }
    
    $this->deletable = $deletable; 
    
    $this->template_content_arr = $template_content_arr;
    
    $this->all_levels = $this->getLevels($template_id);
    
    $this->footer_templates = $this->getTemplatesForFooter($this->template);
    
     
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->template_content = Doctrine_Core::getTable('TemplateContent')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->template_content);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new TemplateContentForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
  	
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    //$form = new TemplateContentForm();

   // $this->processForm($request, $form); 
   
     $content_types = Doctrine_Core::getTable('ContentType')->retreiveActiveContentTypes();
     
     $template_content_arr = array();
     
   	 foreach ($content_types as $content_type) {
    	
    	
    	if ($content_type->getId() != sfConfig::get('app_content_type_footer')){
    		
    		$the_content = $request->getParameter('ta_'.$content_type->getId());
    		
    		$templateContent = Doctrine_Core::getTable('TemplateContent')->getTemplateContent($request->getParameter('template_id'), $content_type->getId());
    		
    		$templateContent = $templateContent == null ? new TemplateContent() : $templateContent;
    		
	    	$templateContent->setContentTypeId($content_type->getId());
	    	
	    	$templateContent->setTemplateId($request->getParameter('template_id'));
	    	
	    	$templateContent->setTheContent($the_content);
	    	
	    	$templateContent->save();
	    	
			$template_content_arr[$content_type->getId()] = $templateContent;
    		
    	}
    }
    
    $template = Doctrine_Core::getTable('Template')->find(array($request->getParameter('template_id')));
    
    $template->setFooterId($request->getParameter('sl_template_footer'));
    
    $template->save();
    
    $all_levels = $this->getLevels($request->getParameter('template_id'));
    
    $this->getUser()->setFlash('test_flash', 'Object saved');

    //return $this->renderPartial('the_content', array('template' =>$template, 'content_types' => $content_types, 'all_levels' => $all_levels, 'template_content_arr' => $template_content_arr, 'deletable' => true));
    return $this->redirect('template_content/index?template_id=' . $request->getParameter('template_id'));
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($template_content = Doctrine_Core::getTable('TemplateContent')->find(array($request->getParameter('id'))), sprintf('Object template_content does not exist (%s).', $request->getParameter('id')));
    $this->form = new TemplateContentForm($template_content);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($template_content = Doctrine_Core::getTable('TemplateContent')->find(array($request->getParameter('id'))), sprintf('Object template_content does not exist (%s).', $request->getParameter('id')));
    $this->form = new TemplateContentForm($template_content);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    //$request->checkCSRFProtection();
    
    $template_id = $request->getParameter('template_id');
    
	$content_types = Doctrine_Core::getTable('ContentType')->retreiveActiveContentTypes();
     
    
   	$template_contents = Doctrine_Core::getTable('TemplateContent')->getTemplateContentByTemplateId($template_id); 
	
   	 foreach ($template_contents as $template_content) {
 		$template_content->delete();
   	 }
    	    
    

    //$this->forward404Unless($template_content = Doctrine_Core::getTable('TemplateContent')->find(array($request->getParameter('id'))), sprintf('Object template_content does not exist (%s).', $request->getParameter('id')));
    //$template_content->delete();

    $this->redirect('templates/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $template_content = $form->save();

      //$this->redirect('template_content/edit?id='.$template_content->getId());
    }
  }
  
  protected function getLevels($template_id){
  	$template2levels = Doctrine_Core::getTable('Template2Level')->getTemplatesLevelsByTemplateId($template_id);
    
    $all_levels = array();
    
    foreach ($template2levels as $template2level){
    	array_push($all_levels, $template2level);
    	foreach ($template2level->getUpperLevel() as $upper_level){
    		array_push($all_levels, $upper_level);
    	}
        foreach ($template2level->getSideLevel() as $side_level){
    		array_push($all_levels, $side_level);
    	}    	
    }
    
    return $all_levels;
  }
  
  protected function getTemplatesForFooter($template){
  	$templates_for_footer = array();
  	
  	$template2levels = $template->getTemplate2Level();
  	$direcctoryId = $template2levels[0]->getLevel()->getCategory()->getMasterDirectoryId();
  	$directory_templates = Doctrine_Core::getTable('Template')->retreiveActiveTemplatesByDirectory($direcctoryId);
  	
  	
  	$main_categories = $this->getTemplateCategories($template);
  	
  	$templates_with_categories = array();
  	
  	foreach ($directory_templates as $directory_template){
  		if($directory_template->getId() != $template->getId()){
  			$categories = $this->getTemplateCategories($directory_template);
  			$templates_with_categories[$directory_template->getId()] = $categories;
  		}
  	}
  	
  	
  	foreach ($templates_with_categories as $template_key => $categories){
  		$common_categories = array_intersect($main_categories, $categories);
  		if(count($categories) == 1 || count($common_categories) > 0){
  			array_push($templates_for_footer, Doctrine_Core::getTable('Template')->find(array($template_key)));
  		}
  	}
  	
  	return $templates_for_footer;
  }
  
	public function getTemplateCategories($template){
  		$arr_categories = array();
  		foreach ($template->getTemplate2Level() as $template2level){
  			array_push($arr_categories, $template2level->getLevel()->getCategory());
  		}
  		return array_unique($arr_categories);
  			
  	}
  	
}
